
const ELEMENT_NODE    = 1;
const ATTRIBUTE_NODE  = 2;
const TEXT_NODE       = 3;
const CDATA_SECTION_NODE    = 4;
const ENTITY_REFERENCE_NODE = 5;
const ENTITY_NODE           = 6;
const PROCESSING_INSTRUCTION_NODE = 7;
const COMMENT_NODE          = 8;
const DOCUMENT_NODE         = 9;
const DOCUMENT_TYPE_NODE    = 10;
const DOCUMENT_FRAGMENT_NODE = 11;
const NOTATION_NODE         = 12;
export function get_nextsibling(x) {
  const stack = new Error().stack;
  const functionName = stack.split('\n')[1].trim();
  if (!x) {
    console.log("[WARNING] no this element:", x);
    return;
  }
  while (x.nodeType != ELEMENT_NODE) {
    x = x.nextSibling;
  }
  return x;
}
//source node: <div id="post0"><input id="id"><input id="name"><button type="submit" id="name0" value="ss">submit</button></div>
//result node: <div><div id="1" name="ss"></div></div>
export function node2xml(parentNode) {
  const stack = new Error().stack;
  const functionName = stack.split('\n')[1].trim();
  //console.log(functionName);

  var node = parentNode.childNodes;
  if (!node) {
    console.log("[WARNING] no this element:", functionName);
    return;
  }
  var dev = document.createElement("div");
  var cell = document.createElement("div");
  dev.appendChild(cell);
  for (const son of node) {
    if (son.nodeType != ELEMENT_NODE) {
      continue;
    }
    if (son.nodeName != 'INPUT') {
      continue;
    }
   // console.log("son:", son.outerHTML);
    //console.log("son nodeName:", son.nodeName);
    cell.setAttribute(son.id, son.value);
  }
  return dev;
}
export function node2xml_str(parentNode) {
  return node2xml(parentNode).outerHTML;
}
//source node: <div id="post0"><input id="id"><input id="name"><button type="submit" id="name0" value="ss">submit</button></div>
//result str: INSERT INTO things (id, name) VALUES (1, ss);
export function node2sql_insert_str(parentNode) {
  const stack = new Error().stack;
  const functionName = stack.split('\n')[1].trim();
  //console.log(functionName);

  var node = parentNode.childNodes;
  if (!node) {
    console.log("[WARNING] no this element:", functionName);
    return;
  }
  var result = 'INSERT INTO things ';
  var values = 'VALUES ';
  var count = 0;
  for (const son of node) {
    if (son.nodeType != ELEMENT_NODE) {
      continue;
    }
    if (son.nodeName != 'INPUT') {
      continue;
    }
    //console.log("son:", son.outerHTML);
    if (count == 0) {
      result += '(';
      values += '(';
    } else {
      result += ', ';
      values += ', ';
    }
    result += son.id;
    values += son.value;
    count++;
  }
  result += ') ' + values + ');';
  return result;
}
export function node2sql_insert_xml_str(parentNode) {
  var div = document.createElement("div");
  var cell = document.createElement("div");
  div.appendChild(cell);
  var str = node2sql_insert_str(parentNode);
  cell.setAttribute("sql", str);
  return div.outerHTML;
}
export function removeLastDigits(str) {
  return str.replace(/\d+$/, '');
}
export function tableNode2sql_update_str(parentNode) {
  const stack = new Error().stack;
  const functionName = stack.split('\n')[1].trim();
  //console.log(functionName);

  var node = parentNode.childNodes;
  if (!node) {
    console.log("[WARNING] no this element:", functionName);
    return;
  }
  var result = 'UPDATE things SET ';
  var condition = ' WHERE ';
  var count = 0;
  for (const son of node) {
    if (son.nodeType != ELEMENT_NODE) {
      continue;
    }
    //console.log("son:", son.outerHTML);
    var input = son.childNodes[0];
    if (input.nodeName != 'INPUT') {
      continue;
    }
    if (count == 0) {
    } else {
      result += ', ';
    }
    var columnName = removeLastDigits(input.id);
    if (columnName == 'id') {
      condition += 'id=' + input.value;
    } else {
      count++;
      result += columnName + '=' + input.value;
    }
  }
  result += condition + ';';
  return result;
}
export function tableNode2sql_update_xml_str(parentNode) {
  var div = document.createElement("div");
  var cell = document.createElement("div");
  div.appendChild(cell);
  var str = tableNode2sql_update_str(parentNode);
  cell.setAttribute("sql", str);
  return div.outerHTML;
}
